<template>
  <basic-dialog
    :visible="dialogVisible"
    :title="'价格管理周期-处理意见审核'"
    :width="'50%'"
    :handleClose="handleCloseDialog"
    :handleOpen="handleOpenDialog"
    :showFooter="false"
  >
    <el-form
      ref="auditForm"
      :model="auditForm"
      label-width="180px"
      v-loading="formLoading"
    >
      <el-form-item label="供应商编码:">
        <span>{{auditForm.supplyUuid}}</span>
      </el-form-item>
      <el-form-item label="供应商名称:">
        <span>{{auditForm.supplyName}}</span>
      </el-form-item>
      <el-form-item label="价格管理周期:">
        <span>{{auditForm.priceManagementCycle}}</span>
      </el-form-item>
      <el-form-item label="是否需调整进价">
        <el-select disabled v-model="auditForm.adjustPriceState">
          <el-option
            v-for="item in PRICE_STATE_OPTIONS"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button
          style="margin-left: 5px"
          v-if="auditForm.adjustPriceState == 2"
          type="primary"
          @click="handleViewDetail"
        >查看明细</el-button>
      </el-form-item>
      <el-form-item label="处理备注说明:" prop="processRemark">
        <el-input disabled v-model.trim="auditForm.processRemark" type="textarea" :rows="2" />
      </el-form-item>
      <el-form-item label="审核说明:" prop="approveRemark">
        <el-input v-model.trim="auditForm.approveRemark" maxlength="200" type="textarea" :rows="2" />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="end">
      <el-button :loading="auditLoading" @click="handleAudit(2)">审核驳回</el-button>
      <el-button type="primary" @click="handleAudit(1)">审核通过</el-button>
    </el-row>
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import { PRICE_STATE_OPTIONS } from '../constants'
import { handleApprovePriceCycleService } from '@/api/finance/priceCycleManage/toDoList';
import { handleOpenNewPage } from '@/utils/bizUtils';

export default {
  name: 'AuditDialog', // 审核
  components: {
    BasicDialog,
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    rowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      formLoading: false,
      auditLoading: false,
      PRICE_STATE_OPTIONS,
      auditForm: {
        adjustPriceTime: '',
        approveRemark: '',
      },
    };
  },
  computed: {
    dialogVisible () {
      return this.visible;
    },
  },
  methods: {
    async handleAudit (auditType) {
      const { approveRemark, recordNo } = this.auditForm
      if (auditType == 2 && !approveRemark) {
        return this.$message.warning('审核驳回须填写审核说明')
      }
      const data = {
        recordNo,
        approveRemark,
        approveState: auditType // 1审核通过、2审核不通过
      }
      const res = await handleApprovePriceCycleService(
        data,
        state => { this.auditLoading = state }
      );
      if (res) {
        this.$message.success('提交成功')
        this.$emit('getTableData')
        this.handleCloseDialog()
      }
    },
    // 获取详情数据
    getDetailData () {
      const {
        recordNo,
        supplyName,
        supplyUuid,
        adjustPriceState,
        priceManagementCycle,
        processRemark,
      } = this.rowData;
      this.auditForm = {
        recordNo,
        supplyName,
        supplyUuid,
        adjustPriceState,
        processRemark,
        priceManagementCycle,
        approveRemark: '',
      };
    },
    // 打开弹窗
    handleOpenDialog () {
      this.getDetailData()
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
    // 查看明细
    handleViewDetail () {
      handleOpenNewPage(this, { name: 'todoSupplyDetail', query: { recordNo: this.auditForm.recordNo, state: '1' } })
    },
  }
};
</script>
